package com.yntx.usercenter.common.config;

import cn.hutool.core.thread.RejectPolicy;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {

    @Bean
    @Qualifier("defaultThreadPool")
    public ExecutorService executorService() {
        ThreadFactory threadFactory = ThreadFactoryBuilder.create().setNamePrefix("default-").build();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100), threadFactory, RejectPolicy.DISCARD.getValue());
        // 使用同ThreadLocal父子线程池
        return TtlExecutors.getTtlExecutorService(threadPoolExecutor);
    }

}
